package com.shushan.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.icu.util.MeasureUnit;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.shushan.shushanbao.R;
import com.shushan.util.DensityUtil;

/**
 * @ClassName: CustomRoundProgressView
 * @Desciption: //自定义圆形进度控件（用于记录中的进度显示）
 * @author: zhangshihao
 * @date: 2018-09-19
 */
public class CustomRoundProgressView extends View {


    private Context mContext;
    /**中心圆的颜色值*/
    private int mCenterRoundColor = Color.argb(255, 255, 255, 255);

    /**进度的颜色*/
    private int mProgressRoundColor = Color.argb(255, 255, 91, 72);
    /**进度的背景颜色*/
    private int mProgressRoundBgColor = Color.argb(255, 217, 217, 217);
    /**进度条的宽度*/
    private int mProgressWidth = 12;
    /**百分数下方的文字*/
    private String mBelowText;


    /***字体颜色*/
    private int mTextColor = Color.rgb(255, 106, 50);
    private float mPencentTextSize = 18;

    private int mWidth,mHeight;
    private int mPaddingX;

    private float mProgress = 0.0f;
    private float mMax = 1.0f;


    private Paint mPaint = new Paint();

    public CustomRoundProgressView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context,attrs);
    }

    public CustomRoundProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public CustomRoundProgressView(Context context) {
        super(context);
        init(context,null);
    }

    public void init(Context context,AttributeSet attrs){
        mContext = context;
        if(attrs == null){
            return;
        }

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomRoundProgressView);
        mCenterRoundColor = typedArray.getColor(R.styleable.CustomRoundProgressView_inner_circle_color,Color.WHITE);
        mProgressRoundColor = typedArray.getColor(R.styleable.CustomRoundProgressView_ring_color,Color.parseColor("#02D86A"));
        mProgressRoundBgColor = typedArray.getColor(R.styleable.CustomRoundProgressView_ring_backgroud_color,Color.argb(38, 88, 226, 116));
        mProgressWidth = (int) typedArray.getDimension(R.styleable.CustomRoundProgressView_stroke_width,(float)DensityUtil.dip2px(context,6f));
        mBelowText = typedArray.getString(R.styleable.CustomRoundProgressView_below_text);
        mPencentTextSize = typedArray.getDimension(R.styleable.CustomRoundProgressView_percent_text_size, DensityUtil.sp2px(context,18f));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mWidth = getWidth();
        mHeight = getHeight();

        if(mWidth > mHeight){
            mWidth = mHeight;
        }
        mPaint.setAntiAlias(true); // 消除锯齿

        int halfWidth = mWidth/6;
        mPaint.setStrokeWidth(mProgressWidth);
        mPaint.setColor(mProgressRoundBgColor);
        mPaint.setStyle(Paint.Style.STROKE);
        RectF oval = new RectF(new Rect(halfWidth+mPaddingX, halfWidth, halfWidth*5+mPaddingX, halfWidth*5));
        canvas.drawArc(oval, 0, 360, false, mPaint);

        mPaint.setColor(mProgressRoundColor);
        canvas.drawArc(oval, -90, -360*mProgress/mMax, false, mPaint);


        halfWidth = mWidth/5;
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mCenterRoundColor);
        oval = new RectF(new Rect(halfWidth+mPaddingX, halfWidth, halfWidth*4+mPaddingX, halfWidth*4));
        canvas.drawArc(oval, 0, 360, false, mPaint);

        mPaint.reset();
        if(TextUtils.isEmpty(mBelowText)) {
            mPaint.setTextSize(mPencentTextSize);
            mPaint.setColor(mTextColor);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setTextAlign(Paint.Align.CENTER);
            String number = String.format("%.0f", mProgress * 100 / mMax);
            canvas.drawText(number, mWidth / 2 + mPaddingX - 22, (mHeight / 2 + mPencentTextSize / 3), mPaint);

            float textWidth = mPaint.measureText(number);
            mPaint.setTextSize(mPencentTextSize);
            canvas.drawText("%", mWidth / 2 + mPaddingX + textWidth / 2 + 7, (mHeight / 2 + mPencentTextSize / 3), mPaint);
        }else{
            mPaint.setTextSize(mPencentTextSize);
            mPaint.setColor(mTextColor);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setTextAlign(Paint.Align.CENTER);
            String number = String.format("%.0f", mProgress * 100 / mMax);
            canvas.drawText(number, mWidth / 2 + mPaddingX - 22, (mHeight / 2 + mPencentTextSize / 3)-20, mPaint);
            mPaint.setColor(Color.parseColor("#C2C2C2"));
            mPaint.setTextSize(DensityUtil.sp2px(mContext,12f));
            canvas.drawText(mBelowText,(mWidth / 2 + mPaddingX - 25)+23,(mHeight / 2 + mPencentTextSize / 3)+20,mPaint);

            float textWidth = mPaint.measureText(number);
            mPaint.setTextSize(mPencentTextSize);
            mPaint.setColor(mTextColor);
            canvas.drawText("%", mWidth / 2 + mPaddingX + textWidth / 2 + 7, (mHeight / 2 + mPencentTextSize / 3)-20, mPaint);
        }
    }

    public void setMax(float mMax) {
        this.mMax = mMax;
    }

    public void setProgress(float mProgress) {
        this.mProgress = mProgress;
        invalidate();
    }

    public float getMax() {
        return mMax;
    }

    public float getProgress() {
        return mProgress;
    }

    public void setmProgressRoundColor(int mProgressRoundColor) {
        this.mProgressRoundColor = mProgressRoundColor;
    }

    public void setmProgressRoundBgColor(int mProgressRoundBgColor) {
        this.mProgressRoundBgColor = mProgressRoundBgColor;
    }

}
